<template>
  <div class='container'>
    <el-row>
      <el-card>
        <el-row>
          <el-col :span="7">
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item size="small" label="学科名称">
              <el-input  v-model="form.subjectName"></el-input>
            </el-form-item>
          </el-form>
          </el-col>
          <el-col :span="9">
            <el-button size="small" style="margin-left: 10px;" @click="form.subjectName=''" >清除</el-button>
            <el-button size="small" type="primary" @click="hGetList">搜索</el-button>
          </el-col>
          <el-col :span="8">
            <div style="text-align: right">
              <el-button size="small" type="success" icon="el-icon-edit" @click="addPost">新增学科</el-button>
            </div>
          </el-col>
        </el-row>
        <TotalityAdd :consts="counts"/>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="48">
          </el-table-column>
          <el-table-column
            prop="subjectName"
            label="学科名称"
            width="100">
          </el-table-column>
          <el-table-column
            prop="username"
            label="创建者"
            width="120">
          </el-table-column>
          <el-table-column
            label="创建日期"
            width="160">
            <template #default="{row}">
              {{row.addDate|Day}}
            </template>
          </el-table-column>
          <el-table-column
            label="前台是否显示">
            <template #default="{row}">
              {{row.isFrontDisplay===1?'是':'否'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="twoLevelDirectory"
            label="二级目录">
          </el-table-column>
          <el-table-column
            prop="tags"
            label="标签">
          </el-table-column>
          <el-table-column
            prop="totals"
            label="题目数量">
          </el-table-column>
          <el-table-column
            label="操作"
            width="240">
            <template #default="{row}">
              <el-link type="primary" @click="onClassify(row)">学科分类</el-link>
              <el-link type="primary">学科标签</el-link>
              <el-link type="primary" @click="xiuGai(row.id)">修改</el-link>
              <el-link type="primary" @click="delFn(row.id)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
       <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          background
          :page-sizes="[5,10,20,30]"
          :page-size="5"
          :total="counts"
          layout="prev, pager, next,sizes, jumper"
        >
        </el-pagination>
      </div>
      </el-card>
    </el-row>
    <SubjectsAdd ref="addForm" :dialog-visible="subjectsShow" @handle-close="handleClose"/>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects'
import SubjectsAdd from '../components/subjects-add'
export default {
  components: {
    SubjectsAdd
  },
  data () {
    return {
      form: {
        subjectName: '',
        page: 1,
        pagesize: 5
      },
      tableData: [],
      counts: null,
      currentPage4: 1,
      subjectsShow: false
    }
  },
  mounted () {
    this.hGetList()
  },
  methods: {
    onClassify (row) {
      this.$router.push({
        path: '/subjects/directorys',
        query: { id: row.id, subjectName: row.subjectName }
      })
    },
    xiuGai (id) {
      this.tableData.forEach(itm => {
        if (itm.id === id) {
          this.$refs.addForm.ruleForm = itm
        }
      })
      this.subjectsShow = true
    },
    delFn (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id })
        this.hGetList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleClose () {
      this.subjectsShow = false
      this.hGetList()
    },
    addPost () {
      this.subjectsShow = true
    },
    async hGetList () {
      const res = await list(this.form)
      this.counts = res.data.counts
      this.tableData = res.data.items
    },
    // 控制条数
    handleSizeChange (val) {
      this.form.pagesize = val
      this.hGetList()
    },
    // 控制页数
    handleCurrentChange (val) {
      this.form.page = val
      this.hGetList()
    }

  }
}
</script>

<style scoped lang='scss'>
.container{
  padding: 15px;
  .el-link{
    margin-left: 10px;
  };
   .block {
    margin-top: 20px;
    text-align: right;
    ::v-deep .el-pagination__jump {
      margin: 0;
    }
  }
}
</style>
